package cn.tedu.sk.finance.income.webapi.service;

import cn.tedu.cgb2202.sk.common.ex.ServiceException;
import cn.tedu.cgb2202.sk.common.web.State;
import cn.tedu.cgb2202.sk.pojo.vo.IncomeSimpleVO;
import cn.tedu.sk.finance.income.service.IncomeService;
import cn.tedu.sk.finance.income.webapi.mapper.IncomeMapper;
import cn.tedu.sk.finance.income.webapi.repository.IncomeRedisRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;

@Service
public class IncomeServiceImpl implements IncomeService {

    @Autowired
    IncomeRedisRepository redisRepository;

    @Autowired
    IncomeMapper incomeMapper;

    @Override
    public void preloadCache() {
        //清除之前的个别缓存
        redisRepository.deleteAllItem();
        //清除List缓存
        Boolean aBoolean = redisRepository.deleteList();
        //从数据库加载缓存
        List<IncomeSimpleVO> byLiseSimpleVO = incomeMapper.getByLiseSimpleVO();
        for (IncomeSimpleVO incomeSimpleVO : byLiseSimpleVO) {
            //后期根据单据编号方便单个数据查询
            redisRepository.save(incomeSimpleVO);
        }
        //加载一个list缓存
        redisRepository.save(byLiseSimpleVO);

    }

    @Override
    public List<IncomeSimpleVO> queryListSimpleVO() {



        List<IncomeSimpleVO> byLiseSimpleVO = incomeMapper.getByLiseSimpleVO();
        if (byLiseSimpleVO == null) {
            throw new ServiceException(State.ERR_INCOME_NOTFOUND,"");
        }

        return null;
    }

    @Override
    public IncomeSimpleVO getBySimpleVO(String documentNr) {

        IncomeSimpleVO byDocumentNr = incomeMapper.getByDocumentNr(documentNr);
        if (byDocumentNr == null) {

            throw new ServiceException(State.ERR_INCOME_DOCUMENT_NOTFOUND,"此单据编号不存在");
        }



        return byDocumentNr;
    }
}
